<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 18-5-26
 * Time: 下午2:57
 */

namespace App\Model\Common;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Auth;
use Validator;

class Module extends BasePaginateModel
{
    public $table = 'nosplit_module';
    public $primaryKey = 'id';
    protected $hidden = ['created_at', 'updated_at'];

    const MPID = ['fang'=>1, 'ke'=> 2];



    public $errors = null;


    public function isValid($params = null){
        if(empty($params)){
            $params = $this->toArray();
        }
        $except = '';
        if(!empty($this->id)){
            $except = ',id,'.$this->id;
        }
        $valid = Validator::make($params, [
            'name'             => 'required|unique:'.$this->table. $except,
        ], [
            'name.required' => '模块名称必填',
            'name.unique' => '模块名称已存在',
        ]);
        $this->errors = $valid->messages();
        return !$valid->fails();
    }

    public function authorities(){
        return $this->hasMany('App\Model\Common\Authority', 'module_id');
    }

    public static function getDownInfo($mId)
    {
        return DB::table('nosplit_authority')->where('module_id', $mId)->value('id');
    }


    /**
     *添加中介公司第一个账号的时候  给该账号分配系统里面所有的权限
     */
    public static function assignAllPermissions()
    {
        $authories  = Redis::get('authoriesall');
        if (!$authories) {
//            $nuData = DB::table('nosplit_module')->where('id', '<>', 26)->select('id', 'nm_type')->get();
            $nuData = DB::table('nosplit_module')->select('id', 'nm_type')->get();
            $authories = '';
            foreach($nuData as $k=>$v) {
                if ($v->nm_type == 1) {//组
                    $naid = DB::table('nosplit_authority')->where('module_id', $v->id)->select('id')->get();
                    foreach($naid as $kone=>$vone) {
                        $authories .= $v->nm_type.'|'.$v->id.'|'.$vone->id.',';
                    }
                } else {//单
                    $naData = DB::table('nosplit_authority')->where('module_id', $v->id)->select('id')->get();
                    foreach ($naData as $key=>$val) {
                        $authories .= $v->nm_type.'|'.$v->id.'|'.$val->id.',';
                    }
                }
            }
            Redis::set('authoriesall',$authories);
        }
        return $authories;
    }


    /**
     * 所有的权限页面数据
     * 权限存储的结构     组|模块ID|权限ID
     */
    public static function PermissionData($authories,$nm_permissions_type)
    {
//        $key = 'qx'.Auth::user()['u_id'].$nm_permissions_type;
//        $data = Redis::get($key);
//        if (!$data) {
            $authoriesArray = explode(',', $authories);
            if (sizeof($authoriesArray) == 0) {
                return false;
            }
            $pid = [];//权限ID
            $mid = [];//组 模块ID
            $mid2 = [];//单 模块的ID
            foreach($authoriesArray as $k=>$v) {
                if ($v) {
                    $nmtype = explode('|', $v);
                    if ($nmtype[0] == 1) {
                        array_push($mid, $nmtype[1]);
                    }else{
                        array_push($mid2, $nmtype[1]);
                        array_push($pid, $nmtype[2]);
                    }
                }
            }
            $mid =array_unique($mid);
            $mid2 =array_unique($mid2);

            //查询下拉框的权限
            $data['zu']  = DB::table('nosplit_module')
                ->whereIn('id', $mid)
                ->where('nm_permissions_type', $nm_permissions_type)
                ->select('id', 'name', 'nm_type', 'nm_drop_down_name')
                ->get();
            foreach($data['zu'] as $k=>$v) {
                $v->pid = DB::table('nosplit_authority')->where('module_id', $v->id)->orderBy('id', 'ASC')->select('id', 'name')->get();
            }

            //查询单选框的权限
            $data['dan']  = DB::table('nosplit_module')
                ->whereIn('id', $mid2)
                ->where('nm_permissions_type', $nm_permissions_type)
                ->select('id', 'name', 'nm_type')
                ->get();
            if (sizeof($data['dan']) != 0) {
                foreach($data['dan'] as $k=>$v) {
                    $v->pid = DB::table('nosplit_authority')->where('module_id', $v->id)->select('id', 'name', 'select_name')->get();
                }
            }

//            $mdata = self::whereIn('id', array_merge($mid,$mid2))
//                ->where('nm_permissions_type', $nm_permissions_type)
//                ->get();
//            $adata = Authority::get();
//            foreach($mdata as $mitem){
//                $tmp = $mitem->toArray();
//                if(in_array($mitem->id, $mid)){
//                    foreach($adata as $aitem){
//                        if($aitem->module_id == $mitem->id){
//                            $tmp['pid'][] = $aitem;
//                        }
//                    }
//                    $data['zu'][] = $tmp;
//                }else if(in_array($mitem->id, $mid2)){
//                    foreach($adata as $aitem){
//                        if($aitem->module_id == $mitem->id){
//                            $tmp['pid'][] = $aitem;
//                        }
//                    }
//                    $data['dan'][] = $tmp;
//                }
//            }

//            Redis::set($key, json_encode($data));
//            Redis::expire($key, env('EXPIRATION'));
//        } else {
//            $data = json_decode($data);
//        }

        return $data;

    }
//    /**
//     * 房源涉及到所有的权限页面数据
//     */
//    public static function roomPermissionData()
//    {
//        //修改的字段权限
//        $data['room']['field']  = Redis::get('permission1');
//        if (!$data['room']['field']) {
//            $data['room']['field'] = DB::table('nosplit_authority')->where('module_id', 1)->select('id', 'name')->get();
//            Redis::set('permission1', $data['room']['field']);
//        }
//        if (!is_array($data['room']['field'])) {
//            $data['room']['field'] = json_decode($data['room']['field']);
//        }
//
//        //房源查看权限
//        $data['room']['look']  = Redis::get('permission2');
//        if (!$data['room']['look']) {
//            $data['room']['look'] = DB::table('nosplit_authority')->where('module_id', 2)->select('id', 'name')->get();
//            Redis::set('permission2', $data['room']['look']);
//        }
//        if (!is_array($data['room']['look'])) {
//            $data['room']['look'] = json_decode($data['room']['look']);
//        }
//
//        //房源添加权限
//        $data['room']['add']  = Redis::get('permission3');
//        if (!$data['room']['add']) {
//            $data['room']['add'] = DB::table('nosplit_authority')->where('module_id', 3)->select('id', 'name')->get();
//            Redis::set('permission3', $data['room']['add']);
//        }
//        if (!is_array($data['room']['add'])) {
//            $data['room']['add'] = json_decode($data['room']['add']);
//        }
//
//        //房源修改权限
//        $data['room']['edit']  = Redis::get('permission4');
//        if (!$data['room']['edit']) {
//            $data['room']['edit'] = DB::table('nosplit_authority')->where('module_id', 4)->select('id', 'name')->get();
//            Redis::set('permission4', $data['room']['edit']);
//        }
//        if (!is_array($data['room']['edit'])) {
//            $data['room']['edit'] = json_decode($data['room']['edit']);
//        }
//
//        //房源软删除的权限
//        $data['room']['del']  = Redis::get('permission5');
//        if (!$data['room']['del']) {
//            $data['room']['del'] = DB::table('nosplit_authority')->where('module_id', 5)->select('id', 'name')->get();
//            Redis::set('permission5', $data['room']['del']);
//        }
//        if (!is_array($data['room']['del'])) {
//            $data['room']['del'] = json_decode($data['room']['del']);
//        }
//
//        //上传房源图片的权限
//        $data['room']['uppic']  = Redis::get('permission6');
//        if (!$data['room']['uppic']) {
//            $data['room']['uppic'] = DB::table('nosplit_authority')->where('module_id', 6)->select('id', 'name')->get();
//            Redis::set('permission6', $data['room']['uppic']);
//        }
//        if (!is_array($data['room']['uppic'])) {
//            $data['room']['uppic'] = json_decode($data['room']['uppic']);
//        }
//
//        //修改房源图片的权限
//        $data['room']['editpic']  = Redis::get('permission7');
//        if (!$data['room']['editpic']) {
//            $data['room']['editpic'] = DB::table('nosplit_authority')->where('module_id', 7)->select('id', 'name')->get();
//            Redis::set('permission7', $data['room']['editpic']);
//        }
//        if (!is_array($data['room']['editpic'])) {
//            $data['room']['editpic'] = json_decode($data['room']['editpic']);
//        }
//
//        //查看跟进（房客源跟进）
//        $data['room']['lookfllow']  = Redis::get('permission8');
//        if (!$data['room']['lookfllow']) {
//            $data['room']['lookfllow'] = DB::table('nosplit_authority')->where('module_id', 8)->select('id', 'name')->get();
//            Redis::set('permission8', $data['room']['lookfllow']);
//        }
//        if (!is_array($data['room']['lookfllow'])) {
//            $data['room']['lookfllow'] = json_decode($data['room']['lookfllow']);
//        }
//
//        //点评跟进（房客源跟进）
//        $data['room']['reviewfllow']  = Redis::get('permission9');
//        if (!$data['room']['reviewfllow']) {
//            $data['room']['reviewfllow'] = DB::table('nosplit_authority')->where('module_id', 9)->select('id', 'name')->get();
//            Redis::set('permission9', $data['room']['reviewfllow']);
//        }
//        if (!is_array($data['room']['reviewfllow'])) {
//            $data['room']['reviewfllow'] = json_decode($data['room']['reviewfllow']);
//        }
//
//        //添加跟进（房客源跟进）
//        $data['room']['addfllow']  = Redis::get('permission10');
//        if (!$data['room']['addfllow']) {
//            $data['room']['addfllow'] = DB::table('nosplit_authority')->where('module_id', 10)->select('id', 'name')->get();
//            Redis::set('permission10', $data['room']['addfllow']);
//        }
//        if (!is_array($data['room']['addfllow'])) {
//            $data['room']['addfllow'] = json_decode($data['room']['addfllow']);
//        }
//
//        //修改跟进（房客源跟进）
//        $data['room']['editfllow']  = Redis::get('permission11');
//        if (!$data['room']['editfllow']) {
//            $data['room']['editfllow'] = DB::table('nosplit_authority')->where('module_id', 11)->select('id', 'name')->get();
//            Redis::set('permission11', $data['room']['editfllow']);
//        }
//        if (!is_array($data['room']['editfllow'])) {
//            $data['room']['editfllow'] = json_decode($data['room']['editfllow']);
//        }
//
//        //删除跟进（房客源跟进）
//        $data['room']['delfllow']  = Redis::get('permission12');
//        if (!$data['room']['delfllow']) {
//            $data['room']['delfllow'] = DB::table('nosplit_authority')->where('module_id', 12)->select('id', 'name')->get();
//            Redis::set('permission12', $data['room']['delfllow']);
//        }
//        if (!is_array($data['room']['delfllow'])) {
//            $data['room']['delfllow'] = json_decode($data['room']['delfllow']);
//        }
//        return $data;
//    }
//
//
//    /**
//     *客源涉及到所有的权限页面数据
//     */
//    public static function customersPermissionData()
//    {
//        //修改的字段权限
//        $data['customers']['field']  = Redis::get('permission13');
//        if (!$data['customers']['field']) {
//            $data['customers']['field'] = DB::table('nosplit_authority')->where('module_id', 13)->select('id', 'name')->get();
//            Redis::set('permission13', $data['customers']['field']);
//        }
//        if (!is_array($data['customers']['field'])) {
//            $data['customers']['field'] = json_decode($data['customers']['field']);
//        }
//
//        //客源查看权限
//        $data['customers']['look']  = Redis::get('permission14');
//        if (!$data['customers']['look']) {
//            $data['customers']['look'] = DB::table('nosplit_authority')->where('module_id', 14)->select('id', 'name')->get();
//            Redis::set('permission14', $data['customers']['look']);
//        }
//        if (!is_array($data['customers']['look'])) {
//            $data['customers']['look'] = json_decode($data['customers']['look']);
//        }
//
//        //客源添加权限
//        $data['customers']['add']  = Redis::get('permission15');
//        if (!$data['customers']['add']) {
//            $data['customers']['add'] = DB::table('nosplit_authority')->where('module_id', 15)->select('id', 'name')->get();
//            Redis::set('permission15', $data['customers']['add']);
//        }
//        if (!is_array($data['customers']['add'])) {
//            $data['customers']['add'] = json_decode($data['customers']['add']);
//        }
//
//        //客源修改权限
//        $data['customers']['edit']  = Redis::get('permission16');
//        if (!$data['customers']['edit']) {
//            $data['customers']['edit'] = DB::table('nosplit_authority')->where('module_id', 16)->select('id', 'name')->get();
//            Redis::set('permission16', $data['customers']['edit']);
//        }
//        if (!is_array($data['customers']['edit'])) {
//            $data['customers']['edit'] = json_decode($data['customers']['edit']);
//        }
//
//        //客源删除权限
//        $data['customers']['del']  = Redis::get('permission17');
//        if (!$data['customers']['del']) {
//            $data['customers']['del'] = DB::table('nosplit_authority')->where('module_id', 17)->select('id', 'name')->get();
//            Redis::set('permission17', $data['customers']['del']);
//        }
//        if (!is_array($data['customers']['del'])) {
//            $data['customers']['del'] = json_decode($data['customers']['del']);
//        }
//        return $data;
//    }
//    /**
//     * 剩余的所有权限页面
//     */
//    public static function otherPermissionData()
//    {
//        //签到管理
//        $data['other']['punch']  = Redis::get('permission18');
//        if (!$data['other']['punch']) {
//            $data['other']['punch'] = DB::table('nosplit_authority')->where('module_id', 18)->select('id', 'name')->get();
//            Redis::set('permission18', $data['other']['punch']);
//        }
//        if (!is_array($data['other']['punch'])) {
//            $data['other']['punch'] = json_decode($data['other']['punch']);
//        }
//
//        //工作总结
//        $data['other']['work']  = Redis::get('permission19');
//        if (!$data['other']['work']) {
//            $data['other']['work'] = DB::table('nosplit_authority')->where('module_id', 19)->select('id', 'name')->get();
//            Redis::set('permission19', $data['other']['work']);
//        }
//        if (!is_array($data['other']['work'])) {
//            $data['other']['work'] = json_decode($data['other']['work']);
//        }
//
//        //合同管理
//        $data['other']['contract']  = Redis::get('permission20');
//        if (!$data['other']['contract']) {
//            $data['other']['contract'] = DB::table('nosplit_authority')->where('module_id', 20)->select('id', 'name')->get();
//            Redis::set('permission20', $data['other']['contract']);
//        }
//        if (!is_array($data['other']['contract'])) {
//            $data['other']['contract'] = json_decode($data['other']['contract']);
//        }
//
//        //诚意金
//        $data['other']['sincerity']  = Redis::get('permission21');
//        if (!$data['other']['sincerity']) {
//            $data['other']['sincerity'] = DB::table('nosplit_authority')->where('module_id', 21)->select('id', 'name')->get();
//            Redis::set('permission21', $data['other']['sincerity']);
//        }
//        if (!is_array($data['other']['sincerity'])) {
//            $data['other']['sincerity'] = json_decode($data['other']['sincerity']);
//        }
//
//        //回收站
//        $data['other']['recycle']  = Redis::get('permission22');
//        if (!$data['other']['recycle']) {
//            $data['other']['recycle'] = DB::table('nosplit_authority')->where('module_id', 22)->select('id', 'name')->get();
//            Redis::set('permission22', $data['other']['recycle']);
//        }
//        if (!is_array($data['other']['recycle'])) {
//            $data['other']['recycle'] = json_decode($data['other']['recycle']);
//        }
//
//        //楼盘字典
//        $data['other']['property']  = Redis::get('permission23');
//        if (!$data['other']['property']) {
//            $data['other']['property'] = DB::table('nosplit_authority')->where('module_id', 23)->select('id', 'name')->get();
//            Redis::set('permission23', $data['other']['property']);
//        }
//        if (!is_array($data['other']['property'])) {
//            $data['other']['property'] = json_decode($data['other']['property']);
//        }
//
//        //部门
//        $data['other']['department']  = Redis::get('permission24');
//        if (!$data['other']['department']) {
//            $data['other']['department'] = DB::table('nosplit_authority')->where('module_id', 24)->select('id', 'name')->get();
//            Redis::set('permission24', $data['other']['department']);
//        }
//        if (!is_array($data['other']['department'])) {
//            $data['other']['department'] = json_decode($data['other']['department']);
//        }
//
//        //人员
//        $data['other']['employee']  = Redis::get('permission25');
//        if (!$data['other']['employee']) {
//            $data['other']['employee'] = DB::table('nosplit_authority')->where('module_id', 25)->select('id', 'name')->get();
//            Redis::set('permission25', $data['other']['employee']);
//        }
//        if (!is_array($data['other']['employee'])) {
//            $data['other']['employee'] = json_decode($data['other']['employee']);
//        }
//        return $data;
//    }

}